package com.zl.payment.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 *
 *
 *
 *
 */
public class DateUtils {
/**
 * 根据出账日计算账户的最后还款日
 * @param billDate
 * @return
 */
    public static Date finalRepaymentDate(Date billDate){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(billDate);
        calendar.add(Calendar.DAY_OF_MONTH,20);
        Date time = calendar.getTime();
        return time;
    }
    //逾期日为当前日期的账单出账日
    public static Date billDate(){
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today());
    calendar.add(Calendar.DATE,-22);
    Date time = calendar.getTime();
    return time;
    }
    //获取当前日期的时间，不包含时分秒
    public static Date today(){
        LocalDate now = LocalDate.now();
        System.out.println(now);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = now.atStartOfDay().atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }
    /**
     * 计算两个日期间的天数
     * @param oldDate
     * @return
     */
        public static int getIntervalDays(Date oldDate){
            Date date = today();
            if (oldDate.before(date)){
                return (int) ((date.getTime()-oldDate.getTime())/(1000*60*60*24));
            }
            return (int) ((oldDate.getTime()-date.getTime())/(1000*60*60*24));
        }

    /**
     * 获取当前日期在每月的第几天
     * @return
     */
    public static int getDayOfMonth(){
        LocalDate now = LocalDate.now();
        int day = now.getDayOfMonth();
        return day;
    }

/**
 * 获取账单月份的字符串yyyyMM
 */

    public static String billMonth(){
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int monthValue = now.getMonthValue();
        if (monthValue<10){
            return year+"0"+monthValue;
        }
        return year+""+monthValue;
    }
    //生成分期账单的账单月份数组
    public static String[] getMonths(Integer num){
        String[] months = new String[num];
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        for (int i = 0; i < num; i++) {
            if (month>=12){
                year++;
                month=0;
            }
            month++;
            if (month<10){
                months[i]= year+"0"+month;
            }else {
                months[i]= year+""+month;
            }
        }
        return months;
    }
}
